var app = getApp()
var host = app.globalData.host
Page({
  data: {
    motto: 'H111112222',
    userInfo: {},
    array:[],
    pageadderss:'',
    pageempty:'',
    left:0,
    leftindex:'',
    selectedAddrId:'',
    comefrom:'',
    adddefid:''
  },
  onLoad: function (options) {
    var that =this
    wx.setNavigationBarTitle({
      title: '我的地址' 
    })
    var comefrom = options.comefrom //来源
    var selectedAddrId = options.addrId //来源处选择的地址id

    console.log('selected Addr Id :' + selectedAddrId)
    if (comefrom && comefrom.length > 0)
    that.setData({
      comefrom: comefrom,
      selectedAddrId: selectedAddrId      
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onRefresh()
  },
  onRefresh(){
    var that = this
    var host = app.globalData.host
    var openId = wx.getStorageSync('openId')

    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: host + "/addressApi/selAddress",
      data: {
        thirdpartyId: openId
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          array: res.data.data
        })
        console.log(that.data.array)
        wx.setStorageSync('addressNum', that.data.array.length)
        if (that.data.array.length == 0) {
          that.setData({
            pageempty: true,
            pageadderss: false
          })
        } else {
          that.setData({
            pageempty: false,
            pageadderss: true
          })
        }
      },
      fail: function (res) {

      },
      complete: function (res) {
        wx.hideLoading()
      }
    })
  },
  // 左划删除
  touchS: function (e) {
    console.log(e)
    this.setData({
      leftindex: e.currentTarget.dataset.index
    })
    if (e.touches.length == 1) {
      this.setData({
        startX: e.touches[0].clientX
      })
    }
  },
  touchM: function (e) {
    var that = this;
    if (e.touches.length == 1) {
      var moveX = e.touches[0].clientX;
      //记录触摸点位置的X坐标
      var moveX = e.touches[0].clientX;
      //计算手指起始点的X坐标与当前触摸点的X坐标的差值
      var disX = that.data.startX - moveX;
      if (disX == 0 || disX < 0) {//如果移动距离小于等于0，文本层位置不变
        that.setData({
          left: 0
        })
      } else if (disX > 0 && disX < 109) {//移动距离大于0，文本层left值等于手指移动距离
        that.setData({
          left: "-" + disX + "rpx"
        })
        if (disX >= 109) {
          //控制手指移动距离最大值为删除按钮的宽度
          that.setData({
            left: "-109rpx"
          })
        }
      }
    }
  },
  touchE: function (e) {
    var that = this;
    if (e.changedTouches.length == 1) {
      //手指移动结束后触摸点位置的X坐标
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离
      var disX = that.data.startX - endX;
      var delBtnWidth = that.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮
      var left = disX > 55 ? "-109rpx" : "0";
      that.setData({
        left: left
      })
    }
  },
  // 去地址编辑页面
  toaddaddress: function (e) {
    var that = this
    wx.navigateTo({
      url: '../personaddress/addaddress?comefrom=' + that.data.comefrom
    })
  },
  // 去地址编辑页面
  toeditaddress:function(e){
    var that = this
    var itemid = e.currentTarget.dataset.index;
    wx.setStorageSync("toEditAddr","xxxx");
    // if(that.data.comefrom != '')
    //   wx.redirectTo({
    //   url: '../personaddress/editaddress?itemid=' + itemid + "&comefrom=" + that.data.comefrom
    // })
    // else 
      wx.redirectTo({
        url: '../personaddress/editaddress?itemid=' + itemid + "&comefrom=" + that.data.comefrom
      })  
  },
  selectAddr: function (e) {
    var that = this
    if (wx.getStorageSync("toEditAddr")){
      wx.removeStorageSync("toEditAddr")
      return ;
    }
    if (that.data.comefrom != ''){
      var itemid = e.currentTarget.dataset.index;
      wx.setStorageSync("selectedAddr", that.data.array[itemid])
      console.info("set storaage :" + that.data.array[itemid].name)
      wx.navigateBack({
        delta: 1
      })
    }
  },
  // 删除地址
  deleteaddress:function (e) {
    console.log(111)
    var that = this
    var source = e.currentTarget.dataset
    var addrId = source.index
    var isDefault = source.defaultindex
    console.log(addrId)
    wx.request({
      url: host + "/addressApi/delAddress",
      data: {
        thirdpartyId: wx.getStorageSync('openId'),
        isDefault: isDefault,
        id: addrId
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.showToast({
          title: '删除成功',
          icon: 'success',
          duration: 2000
        })
        wx.navigateBack({
          delta: 1
        })
      }
    })
  },
  setdefaultaddress: function (e) {
    var that = this;
    var adddefid = e.currentTarget.dataset.defaultaddid
    this.setData({
      adddefid: adddefid
    })
    wx.request({
      url: host + '/addressApi/setDefault',
      data:{
        id: adddefid
      },
      header: {
        'content-type': 'application/json'
      }, success: function (res) {
        that.onRefresh()
      }
    })
  }
})
